﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.DataAccess;
using System.Text.RegularExpressions;

namespace SD.Business
{
    public partial class ContactBUS
    {
        /// <summary>
        /// Get list contact
        /// </summary>
        /// <param name="maximumRows">max record that we want to get</param>
        /// <param name="startRowIndex">start row index</param>
        /// <param name="sortType">sort type</param>
        /// <param name="filter">Filter the state of contact. True is active, False is unactive</param>
        /// <param name="firstName">first name</param>
        /// <param name="surName">surname</param>
        /// <returns></returns>
        public List<Contact> GetListContact(int maximumRows, int startRowIndex, string sortType = null, bool filter = false,
            string firstName = null, string surName = null)
        {
            // author: ToanTN, version: 1.0, Date: 11 Dec 2012
            // Modified by: ToanTN, Date: 13 Dec 2012, add sort function.
            return Contact.GetListContact(filter, firstName, surName, maximumRows, startRowIndex, sortType);
        }

        /// <summary>
        /// Count contact
        /// </summary>
        /// <param name="filter">Filter the state of contact. True is active, False is unactive</param>
        /// <param name="firstName">First name</param>
        /// <param name="surName">Surname</param>
        /// <returns></returns>
        public int CountContact(bool filter = false, string firstName = null, string surName = null)
        {
            // author: ToanTN, version: 1.0, Date: 11 Dec 2012
            return Contact.CountContacts(filter, firstName, surName);
        }

        /// <summary>
        /// Selects all best method contact
        /// </summary>
        /// <returns>A queryable best method contacts</returns>
        public static IQueryable<BestContactMethod> SelectAllBestMethodContacts()
        {
            // author: ToanTN, version: 1.0, Date: 11 Dec 2012
            return Contact.SelectAllBestMethodContacts();
        }

        /// <summary>
        /// Selects all contact type
        /// </summary>
        /// <returns>A queryable contact types</returns>
        public static IQueryable<ContactType> SelectAllContactTypes()
        {
            // author: ToanTN, version: 1.0, Date: 11 Dec 2012
            return Contact.SelectAllContactTypes();
        }

        /// <summary>
        /// Get contact by contact id
        /// </summary>
        /// <param name="contactId">contact id</param>
        /// <returns>A contact</returns>
        public static Contact GetContactById(int contactId)
        {
            // author: ToanTN, version: 1.0, Date: 11 Dec 2012
            return Contact.GetContactById(contactId);
        }

        /// <summary>
        /// Validate information of contact
        /// </summary>
        /// <param name="contact">the contact that we want to validate</param>
        /// <returns>True: success, False: fail</returns>
        public static bool ValidateInput(Contact contact)
        {
            // author: ThungNN, version: 1.0, Date: 13 Dec 2012
            // validate Null Value
            if (string.IsNullOrEmpty(contact.FirstName)) // first name is null or empty
            {
                return false;
            }
            if (string.IsNullOrEmpty(contact.Surname)) // surname is null or empty
            {
                return false;
            }
            if (string.IsNullOrEmpty(contact.WorkBase)) // workbase is null or empty
            {
                return false;
            }

            if (!string.IsNullOrEmpty(contact.Email))
            {
                // validate email
                // reference http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-
                string emailPattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                Regex re = new Regex(emailPattern);
                if (!re.IsMatch(contact.Email)) // email is not match
                {
                    return false;
                }    
            }
            

            // validate phone Value
            string phonePattern = "^([0-9\\s\\-])+$";
            Regex regexPhone = new Regex(phonePattern);
            if (!string.IsNullOrEmpty(contact.OfficePhone)) // office phone is not null or empty
            {
                if (!regexPhone.IsMatch(contact.OfficePhone)) // office phone is not match
                {
                    return false; 
                }
            }
            if (!string.IsNullOrEmpty(contact.MobilePhone)) // mobile phone is not null or empty
            {
                if (!regexPhone.IsMatch(contact.MobilePhone)) // mobile phone is not match
                {
                    return false;
                }
            }
            if (!string.IsNullOrEmpty(contact.HomePhone)) // home phone is not null or empty
            {
                if (!regexPhone.IsMatch(contact.HomePhone)) // contact phone is not match
                {
                    return false;    
                }                
            }
            if (contact.ContactTypeId <= 0)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// Insert a contact into DB
        /// </summary>
        /// <param name="contact">contact to insert</param>
        /// <returns>True: success, False: fail</returns>
        public static bool InsertContact(Contact contact)
        {
            // author: ToanTN, version: 1.0, Date: 12 Dec 2012
            try
            {
                bool resultValidation = ValidateInput(contact);
                if (resultValidation) // validate successfully
                {
                    Contact.InsertContact(contact); // insert
                    return true;
                }
                else // validate fail
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Update contact
        /// </summary>
        /// <param name="contact">A contact to update</param>
        /// <returns>True: success, False: fail</returns>
        public static bool UpdateContact(Contact contact)
        {
            // author: ToanTN, version: 1.0, Date: 12 Dec 2012
            try
            {
                // validate contact inforation
                bool resultValidation = ValidateInput(contact);
                if (resultValidation) // success validation
                {
                    // update
                    bool resultUpdate = Contact.UpdateContact(contact);
                    return resultUpdate;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
